//
//  XCPullMenu.m
//  Project
//
//  Created by 苟亚明 on 16/6/3.
//
//

#import "XCPullMenu.h"
#import "menuCell.h"
#define TopToView 10.0f
#define LeftToView 10.0f
#define CellLineEdgeInsets UIEdgeInsetsMake(0, 10, 0, 10)

@interface XCPullMenu()
@property (nonatomic, assign) CGPoint origin;
@property (nonatomic, assign) CGFloat rowHeight;
@property (nonatomic, strong) UIView *backView;
@end
@implementation XCPullMenu

- (instancetype)initWithDataArr:(NSArray *)dataArr origin:(CGPoint)origin width:(CGFloat)width rowHeight:(CGFloat)rowHeight
{
    self = [super initWithFrame:CGRectMake(0, 0, GYMScreenW, GYMScreenH)];
    if (self) {
        if (rowHeight <= 0) {
            rowHeight = 44/667.0*GYMScreenH;
        }
        self.backgroundColor = [UIColor clearColor];
        
        self.origin = origin;
        
        self.rowHeight = rowHeight;
        
        self.arrData = [dataArr copy];
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x + LeftToView, origin.y + TopToView, width, 306) style:UITableViewStylePlain];
        self.tableView.height = dataArr.count*_rowHeight+rowHeight>306?306:dataArr.count*_rowHeight;
        _tableView.dataSource = self;
        
        _tableView.delegate = self;
        
        [self addSubview:_tableView];
        
        _tableView.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
        
     //   _tableView.layer.cornerRadius = 2;
        
        _tableView.bounces = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        
//        _tableView.separatorColor = [UIColor colorWithWhite:0.3 alpha:1];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"XCPullMenu"];
        [_tableView registerNib:[UINib nibWithNibName:@"menuCell" bundle:nil] forCellReuseIdentifier:@"menuCellId"];
       
//        [self createheadAndfoot];
        
        if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
            
            [self.tableView setSeparatorInset:CellLineEdgeInsets];
            
        }
        
        if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
            
            [self.tableView setLayoutMargins:CellLineEdgeInsets];
            
        }
        
    }
    
    return self;
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    UIView *backView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    backView.alpha = 0.5;
    backView.backgroundColor = [UIColor blackColor];
    [self insertSubview:backView atIndex:0];
    self.backView = backView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.rowHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    menuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"menuCellId"];
    
    cell.backgroundColor = [UIColor clearColor];

    cell.lblTitle.font = [UIFont systemFontOfSize:15];
    
    cell.lblTitle.text = self.arrData[indexPath.row];
    
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    
    cell.selectedBackgroundView.backgroundColor = [UIColor blackColor];
    
    cell.textLabel.textColor = [UIColor lightGrayColor];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([self.delegate respondsToSelector:@selector(XCPullMenu:didSelectRowAtIndexPath:)]){
        [self.delegate XCPullMenu:tableView didSelectRowAtIndexPath:indexPath];
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self dismissWithCompletion:nil];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:CellLineEdgeInsets];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:CellLineEdgeInsets];
        
    }
    
}

- (void)dismissWithCompletion:(void (^)(XCPullMenu *object))completion
{
    __weak __typeof(self) weakSelf = self;
    
    [UIView animateWithDuration:0.2 animations:^{
        
        weakSelf.alpha = 0;
        
        weakSelf.tableView.frame = CGRectMake(weakSelf.origin.x + LeftToView, weakSelf.origin.y + TopToView, 0, 0);
        
        self.backView.alpha = 0;
        
    } completion:^(BOOL finished) {
        
        [weakSelf removeFromSuperview];
        
        if (completion) {
            
            completion(weakSelf);
        }
        if (weakSelf.dismiss) {
            
            weakSelf.dismiss();
        }
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    UITouch *touch = [touches anyObject];
    
    if (![touch.view isEqual:self.tableView]) {
        [self dismissWithCompletion:nil];
    }
}
@end